package com.modelscope.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.modelscope.ai.domain.entity.Article;
import com.modelscope.ai.domain.entity.ArticleItem;
import com.modelscope.ai.domain.entity.SubscribeArticle;
import com.modelscope.ai.domain.entity.Visit;
import com.modelscope.ai.domain.vo.VisitVo;
import com.modelscope.ai.mapper.ArticleItemMapper;
import com.modelscope.ai.mapper.ArticleMapper;
import com.modelscope.ai.mapper.SubscribeArticleMapper;
import com.modelscope.ai.service.VisitService;
import com.modelscope.ai.mapper.VisitMapper;
import com.modelscope.ai.utils.AssertUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 * @description 针对表【visit】的数据库操作Service实现
 * @createDate 2024-03-15 10:56:30
 */
@Service
@RequiredArgsConstructor
public class VisitServiceImpl extends ServiceImpl<VisitMapper, Visit> implements VisitService {


    final ArticleItemMapper articleItemMapper;
    final ArticleMapper articleMapper;
    final SubscribeArticleMapper subscribeArticleMapper;

    /**
     * 新增访问合集
     *
     * @param userId
     * @param articleItemId
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addVisited(Long userId, Long articleItemId) {
        ArticleItem articleItem = articleItemMapper.selectById(articleItemId);
        AssertUtil.isNotEmpty(articleItem, "文章id不存在");

        // 首先 是否已经添加合集了
        SubscribeArticle subscribeArticle = subscribeArticleMapper.selectOne(
                new LambdaQueryWrapper<SubscribeArticle>().eq(SubscribeArticle::getUid, userId)
                        .eq(SubscribeArticle::getArticleId, articleItem.getPid())
        );
        //该文章已经添加合集了
        if (Objects.isNull(subscribeArticle)) {
            return;
        }
        Visit one = lambdaQuery().eq(Visit::getUserId, userId).eq(Visit::getArticleItemid, articleItemId).one();
        if (one == null) {
            Visit add = new Visit();
            add.setUserId(userId);
            add.setPid(subscribeArticle.getId());
            add.setArticleId(articleItem.getPid());
            add.setArticleItemid(articleItemId);
            save(add);
        }
    }

//    /**
//     * 新添加合集
//     *
//     * @param userId
//     * @param articleId
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public void add(Long userId, Long articleId) {
//        Article article = articleMapper.selectById(articleId);
//        AssertUtil.isNotEmpty(article, "文章id不存在");
//        Visit one = lambdaQuery().eq(Visit::getUserId, userId).eq(Visit::getArticleId, articleId).one();
//        if (one == null) {
//            Visit add = new Visit();
//            add.setUserId(userId);
//            add.setArticleId(articleId);
//            save(add);
//        }
//    }


    @Override
    public List<VisitVo> getVisited(Long userId) {
        return getBaseMapper().getVisited(userId);
    }
}




